﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Move
{
    public partial class FrmMoveVirtualShelf : Form
    {
        public FrmMoveVirtualShelf()
        {
            InitializeComponent();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                upload();
            }
        }
        BarcodeUtils obj = new BarcodeUtils();

        void upload()
        {
            BarcodeUtils.BarObject barobj = obj.AnalysisBarcode(Utils.AnalysisSerPath, txt_reelid.Text);

            Utils.uContext.Accountk__BackingField = "mes40";
            StringBuilder str = new StringBuilder();
            str.Append(string.Format(@"select housecode,wocode,reelid,pocode,partnumber,qty,batch1,isnull(shelfid,'') as shelfid from MdcDatVStorage where ReelID='{0}' and housecode in('007','008') ", barobj.reelId));
            DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, str.ToString(), new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt.Rows.Count == 0)
            {
                lab_msg.Text = "料卷非待调拨状态";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            else
            {
                //if (dt.Rows[0]["housecode"].ToString() != "007" && dt.Rows[0]["housecode"].ToString() != "008")
                //{
                //    lab_msg.Text = "料卷已被调拨，请勿重复调拨";
                //    lab_msg.ForeColor = Color.Red;
                //    txt_reelid.SelectAll();
                //    txt_reelid.Focus();
                //    return;
                //}
                //判断是否在货架上
                if (dt.Rows[0]["shelfid"] != null && dt.Rows[0]["shelfid"].ToString() != "")
                {
                    lab_msg.Text = "料卷已经上架.";
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    return;
                }
                #region 注释
                ////获取供应商信息
                //string po = dt.Rows[0]["pocode"].ToString(); 
                #endregion

                //料号
                string partNumber = dt.Rows[0]["partnumber"].ToString();
                string sql_isSendCheck = string.Format(@"select IsSendCheck  from dbo.MdcdatMaterial where MaterialCode='{0}' and IsSendCheck='是'", partNumber);
                DataTable dt_isSendCheck = Utils.NMS.QueryDataTable(Utils.uContext, sql_isSendCheck, new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt_isSendCheck.Rows.Count > 0)//需要送检
                {
                    string sql_CheckStatus = string.Format(@"
declare @docnumber nVarchar(50)--单据号
declare @status nVarchar(50)--检验状态
declare @receive nVarchar(50)--是否接收
if not exists(select *  from dbo.T_PartNumber_Check where ReelID='{0}')
	begin
		select '0','料盘未注册' return
	end
else
	begin
		select @docnumber=DocNum from dbo.T_PartNumber_Check where ReelID='{0}'
		select @status=Status,@receive=Recive from dbo.MdcDatIQCCheck where DocNum=@docnumber
		select '1',@status as 'Status',@receive as 'Receive',@docnumber as 'docnumber'
		return		
	end", barobj.reelId);
                    DataTable dt_CheckStatus = Utils.NMS.QueryDataTable(Utils.uContext, sql_CheckStatus, new CIT.WMS.MESWcf.CmdParameter[0]);
                    if (dt_CheckStatus.Rows[0][0].ToString() == "0")
                    {
                        lab_msg.Text = dt_CheckStatus.Rows[0][1].ToString();
                        lab_msg.ForeColor = Color.Red;
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                    else
                    {
                        switch (dt_CheckStatus.Rows[0]["Status"].ToString())
                        {
                            case "0":
                                lab_msg.Text = "IQC待检状态.";
                                lab_msg.ForeColor = Color.Red;
                                txt_reelid.SelectAll();
                                txt_reelid.Focus();
                                break;
                            case "2":
                                lab_msg.Text = "IQC抽样完成状态.";
                                lab_msg.ForeColor = Color.Red;
                                txt_reelid.SelectAll();
                                txt_reelid.Focus();
                                break;
                            case "3":
                                lab_msg.Text = "IQC开始抽样状态.";
                                lab_msg.ForeColor = Color.Red;
                                txt_reelid.SelectAll();
                                txt_reelid.Focus();
                                break;
                            case "4":
                                lab_msg.Text = "IQC开始检验状态.";
                                lab_msg.ForeColor = Color.Red;
                                txt_reelid.SelectAll();
                                txt_reelid.Focus();
                                break;
                            default:
                                break;
                        }
                        if (dt_CheckStatus.Rows[0]["Status"].ToString() != "1")
                        {
                            return;
                        }
                        else if (dt_CheckStatus.Rows[0]["Status"].ToString() == "1")
                        {
                            if (dt_CheckStatus.Rows[0]["Receive"].ToString() == "0" || dt_CheckStatus.Rows[0]["Receive"].ToString() == "3" || dt_CheckStatus.Rows[0]["Receive"].ToString() == "4")
                            {//begin 1
                                InputCheckToWmsAction(barobj, dt, dt_CheckStatus);
                            }//end 1
                            else if (dt_CheckStatus.Rows[0]["Receive"].ToString() == "1")
                            {
                                lab_msg.Text = "IQC检验失败,不可入库.";
                                lab_msg.ForeColor = Color.Red;
                                txt_reelid.SelectAll();
                                txt_reelid.Focus();
                                return;
                            }
                            else if (dt_CheckStatus.Rows[0]["Receive"].ToString() == "2")
                            {
                                lab_msg.Text = "IQC检验状态未知.";
                                lab_msg.ForeColor = Color.Red;
                                txt_reelid.SelectAll();
                                txt_reelid.Focus();
                                return;
                            }
                        }
                    }
                }
                else//不需要送检
                {
                    string sqlNoCheckToWms = string.Format(@"
begin try
update mdcdatvstorage set  housecode='001' where reelid='{0}'
if not exists (select * from MdcDatRStorage where PartNumber='{2}')
	begin
	    insert into MdcDatRStorage(PartNumber,Qty)values('{2}','0')
	end   
		update mdcdatrstorage set qty=qty+'{1}' where partnumber='{2}'
select '1','调拨成功'
end try
begin catch
	select '0','调拨失败'
end catch
", barobj.reelId, dt.Rows[0]["qty"].ToString(), dt.Rows[0]["partnumber"].ToString());
                    DataTable dt_noCheck = Utils.NMS.QueryDataTable(Utils.uContext, sqlNoCheckToWms, new CIT.WMS.MESWcf.CmdParameter[0]);
                    if (dt_noCheck.Rows[0][0].ToString() == "1")
                    {
                        Utils.SaveAppropriationSheet(new AppropriationSheet() { MType = "2", PartNumber = dt.Rows[0]["partnumber"].ToString(), WOCode = dt.Rows[0]["wocode"].ToString(), POCode = string.Empty, ReelID = barobj.reelId, Qty = dt.Rows[0]["qty"].ToString(), Description = "调拨做账", HouseCode = "001", FromHouse = "008", User = Utils.uContext.UserNamek__BackingField });

                        lab_msg.Text = "调拨成功.";
                        lab_msg.ForeColor = Color.Lime;
                        txt_reelid.Text = "";
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                    else
                    {
                        lab_msg.Text = dt_noCheck.Rows[0][1].ToString();
                        lab_msg.ForeColor = Color.Red;
                        txt_reelid.Text = "";
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                }
                #region MyRegion
                ////判断IQC检验结果是否可以入库
                ////判断是自购料还是客供料
                //string supplyType = "select top 1 SuppliyType,status,Recive from MdcDatIQCCheck where po='" + po + "'  and pn = '" + dt.Rows[0]["partnumber"].ToString() + "' order by updatetime desc --and lot='" + dt.Rows[0]["batch1"].ToString() + "'";
                //string supty;
                //DataTable dt_suptype = Utils.NMS.QueryDataTable(Utils.uContext, supplyType, new CIT.WMS.MESWcf.CmdParameter[0]);
                //DataTable dt_checkReceive = dt_suptype;
                //if (dt_suptype != null && dt_suptype.Rows.Count == 0)
                //{
                //    lab_msg.Text = "PO不存在或PO中无此料";
                //    lab_msg.ForeColor = Color.Red;
                //    txt_reelid.SelectAll();
                //    txt_reelid.Focus();
                //    return;
                //    supty = dt_suptype.Rows[0][0].ToString();
                //    //if (supty == "1")
                //    //{
                //    //    string checkReceive = "select status,Recive from MdcDatIQCCheck where po='" + po + "' and pn='" + dt.Rows[0]["partnumber"].ToString() + "' --and lot='" + dt.Rows[0]["batch1"].ToString() + "'";
                //    //    dt_checkReceive = Utils.NMS.QueryDataTable(Utils.uContext, checkReceive, new CIT.WMS.MESWcf.CmdParameter[0]);

                //    //}
                //    //else
                //    //{
                //    //    string checkReceive = "select status,Recive from MdcDatIQCCheck where po='" + po + "' --and lot='" + dt.Rows[0]["batch1"].ToString() + "'";
                //    //    dt_checkReceive = Utils.NMS.QueryDataTable(Utils.uContext, checkReceive, new CIT.WMS.MESWcf.CmdParameter[0]);

                //    //}
                //}


                //if (dt_checkReceive != null && dt_checkReceive.Rows.Count > 0)
                //{
                //    if (dt_checkReceive.Rows[0]["status"].ToString() == "0")
                //    {
                //        lab_msg.Text = "IQC待检状态.";
                //        lab_msg.ForeColor = Color.Red;
                //        txt_reelid.SelectAll();
                //        txt_reelid.Focus();
                //        return;
                //    }
                //    else if (dt_checkReceive.Rows[0]["status"].ToString() == "2")
                //    {
                //        lab_msg.Text = "IQC抽样完成状态.";
                //        lab_msg.ForeColor = Color.Red;
                //        txt_reelid.SelectAll();
                //        txt_reelid.Focus();
                //        return;
                //    }
                //    else if (dt_checkReceive.Rows[0]["status"].ToString() == "3")
                //    {
                //        lab_msg.Text = "IQC开始抽样状态.";
                //        lab_msg.ForeColor = Color.Red;
                //        txt_reelid.SelectAll();
                //        txt_reelid.Focus();
                //        return;
                //    }
                //    else if (dt_checkReceive.Rows[0]["status"].ToString() == "4")
                //    {
                //        lab_msg.Text = "IQC开始检验状态.";
                //        lab_msg.ForeColor = Color.Red;
                //        txt_reelid.SelectAll();
                //        txt_reelid.Focus();
                //        return;
                //    }
                //    else if (dt_checkReceive.Rows[0]["status"].ToString() == "1")
                //    {
                //        if (dt_checkReceive.Rows[0]["Recive"].ToString() == "0" || dt_checkReceive.Rows[0]["Recive"].ToString() == "3" || dt_checkReceive.Rows[0]["Recive"].ToString() == "4")
                //        {
                //            decimal qty = 0;
                //            try
                //            {
                //                qty = Convert.ToDecimal(dt.Rows[0]["qty"].ToString());
                //            }
                //            catch
                //            {

                //            }
                //            //DataTable dt_check = Utils.NMS.QueryDataTable(Utils.uContext, "sp_CheckPOQty '" + po + "','" + dt.Rows[0]["partnumber"].ToString() + "','" + qty + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
                //            //if (dt_check.Rows[0][0].ToString() == "0")
                //            //{
                //            //    string strQty = "";
                //            //    if (dt_check.Columns.Count == 3)
                //            //    {
                //            //        strQty = "【" + dt_check.Rows[0][2].ToString() + "】";
                //            //    }
                //            //    lab_msg.Text = dt_check.Rows[0][1].ToString() + strQty;
                //            //    lab_msg.ForeColor = Color.Red;
                //            //    txt_reelid.SelectAll();
                //            //    txt_reelid.Focus();
                //            //    return;
                //            //}

                //            //判断物料是客供料还是自购料
                //            DataTable dt_check = Utils.NMS.QueryDataTable(Utils.uContext, "sp_POinputWMS '" + po + "','" + dt.Rows[0]["partnumber"].ToString() + "','" + qty + "','" + barobj.reelId + "','" + Utils.uContext.UserNamek__BackingField + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
                //            if (dt_check.Rows[0][0].ToString() == "1")
                //            {
                //                Utils.SaveAppropriationSheet(new AppropriationSheet() { MType = "2", PartNumber = dt.Rows[0]["partnumber"].ToString(), WOCode = dt.Rows[0]["wocode"].ToString(), POCode = po, ReelID = barobj.reelId, Qty = dt.Rows[0]["qty"].ToString(), Description = "调拨做账", HouseCode = "027", FromHouse = "007", User = Utils.uContext.UserNamek__BackingField });

                //                lab_msg.Text = "调拨成功.";
                //                lab_msg.ForeColor = Color.Lime;
                //                txt_reelid.Text = "";
                //                txt_reelid.SelectAll();
                //                txt_reelid.Focus();
                //                return;
                //            }
                //            else
                //            {
                //                string strQty = "";
                //                if (dt_check.Columns.Count == 3)
                //                {
                //                    strQty = "【" + dt_check.Rows[0][2].ToString() + "】";
                //                }
                //                lab_msg.Text = dt_check.Rows[0][1].ToString() + strQty;
                //                lab_msg.ForeColor = Color.Red;
                //                txt_reelid.Text = "";
                //                txt_reelid.SelectAll();
                //                txt_reelid.Focus();
                //                return;
                //            }
                //        }
                //        else if (dt_checkReceive.Rows[0]["Recive"].ToString() == "1")
                //        {
                //            lab_msg.Text = "IQC检验失败,不可入库.";
                //            lab_msg.ForeColor = Color.Red;
                //            txt_reelid.SelectAll();
                //            txt_reelid.Focus();
                //            return;
                //        }
                //        else if (dt_checkReceive.Rows[0]["Recive"].ToString() == "2")
                //        {
                //            lab_msg.Text = "IQC检验状态未知.";
                //            lab_msg.ForeColor = Color.Red;
                //            txt_reelid.SelectAll();
                //            txt_reelid.Focus();
                //            return;
                //        }
                //    }
                //}
                //else
                //{
                //    lab_msg.Text = "未获取到IQC检验记录";
                //    lab_msg.ForeColor = Color.Red;
                //    txt_reelid.SelectAll();
                //    txt_reelid.Focus();
                //    return;
                //} 
                #endregion

            }
        }

        private void InputCheckToWmsAction(BarcodeUtils.BarObject barobj, DataTable dt, DataTable dt_CheckStatus)
        {
            decimal qty = 0;
            try
            {
                qty = Convert.ToDecimal(dt.Rows[0]["qty"].ToString());
            }
            catch
            {

            }
            DataTable dt_check = Utils.NMS.QueryDataTable(Utils.uContext, "[sp_inputCheckToWMS] '" + dt_CheckStatus.Rows[0]["docnumber"].ToString() + "','" + dt.Rows[0]["partnumber"].ToString() + "','" + qty + "','" + barobj.reelId + "','" + Utils.uContext.UserNamek__BackingField + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt_check.Rows[0][0].ToString() == "1")
            {
                Utils.SaveAppropriationSheet(new AppropriationSheet() { MType = "2", PartNumber = dt.Rows[0]["partnumber"].ToString(), WOCode = dt.Rows[0]["wocode"].ToString(), POCode = dt_CheckStatus.Rows[0]["docnumber"].ToString(), ReelID = barobj.reelId, Qty = dt.Rows[0]["qty"].ToString(), Description = "调拨做账", HouseCode = "001", FromHouse = "007", User = Utils.uContext.UserNamek__BackingField });

                lab_msg.Text = "调拨成功.";
                lab_msg.ForeColor = Color.Lime;
                txt_reelid.Text = "";
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            else
            {
                string strQty = "";
                if (dt_check.Columns.Count == 3)
                {
                    strQty = "【" + dt_check.Rows[0][2].ToString() + "】";
                }
                lab_msg.Text = dt_check.Rows[0][1].ToString() + strQty;
                lab_msg.ForeColor = Color.Red;
                txt_reelid.Text = "";
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
        }

        private void btn_yes_Click(object sender, EventArgs e)
        {
            upload();
        }
    }
}